package com.yuan.fmmall.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.yuan.fmmall.service.ProductCommontsService;
import com.yuan.fmmall.service.ProductService;
import com.yuan.fmmall.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Yuan
 */

@RestController
@CrossOrigin
@RequestMapping("/product")
@Api(value = "提供商品信息相关的接口",tags = "商品管理")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductCommontsService productCommontsService;

    @ApiOperation("商品基本信息查询接口")
    @GetMapping("/detail-info/{productId}")
    public ResultVO getProductbasicInfo(@PathVariable("productId") String productId){
        try {
            return productService.getProductBasicInfo(productId);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @ApiOperation("商品参数信息查询接口")
    @GetMapping("/params-info/{productId}")
    public ResultVO getProductParamsById(@PathVariable("productId") String productId){
        return productService.getProductParamsById(productId);
    }

    @ApiOperation("商品评论信息查询接口")
    @GetMapping("/detail-commonts/{productId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "查看哪一页",required = true,dataType = "int"),
            @ApiImplicitParam(name = "limit",value = "每页显示的评论数",required = true,dataType = "int"),
            @ApiImplicitParam(name = "productId",value = "查询的商品ID",required = true,dataType = "String")
    })
    public ResultVO getProductCommonts(String productId, int pageNum, int limit){
        return productCommontsService.listCommontsByProductId(productId,pageNum,limit);
    }

    @ApiOperation("统计当前商品的好评/中评/差评")
    @GetMapping("/count-commonts/{productId}")
    @ApiImplicitParam(name = "productId",value = "商品ID",required = true,dataType = "String")
    public ResultVO getCommentsCountByProductId(String productId){
        return productCommontsService.getCommentsCountByProductId(productId);
    }
}
